#!/bin/bash

TIMEOUT=10
LINGER=10

# create pipes

tmpfile="lsamod$RANDOM$RANDOM$RANDOM"

mkfifo $tmpfile
exec 5<>$tmpfile
rm -f $tmpfile

mkfifo $tmpfile
exec 6<>$tmpfile
rm -f $tmpfile

# get rid of annoying error messages
exec 2> /dev/null

#echo "Log opened" >/home/squid/log

while read ; do
    netcat $1 $2 <&5 >&6 5>&- 6>&- &

    while true ; do
        # we assume netcat is working within this loop

        #echo "Resolving $REPLY" >>/home/squid/log

        echo $REPLY | sed 's/"\([^"]*\)":"[^"]*"/\1/' >&5
        if read -t $TIMEOUT <&6 ; then
            echo $REPLY
        else
            echo "ERR network error"
            kill -9 %1
            break;
        fi

        if ! read -t $LINGER ; then
            kill -9 %1
            break;
        fi
    done
done

exit 0

